import Toast from 'tdesign-miniprogram/toast/index';
import {wxRegister, getEssayMessage, getOrderMessage, getInfo} from '../../utils/request'

const menuData = [
  [
    {
      index: 0,
      title: '地址簿',
      tit: '',
      url: '',
      type: 'address',
    },
    {
      index: 2,
      title: '我的需求',
      tit: '',
      url: '',
      type: 'essay',
    },
    {
      index: 3,
      title: '客服热线',
      tit: '',
      url: '',
      type: 'service',
      icon: 'service',
    },
  ]
];

const orderTagInfos = [
  {
    title: '待接单',
    iconName: 'hourglass',
    orderNum: 0,
    tabType: 1,
    status: 1,
  },
  {
    title: '待支付',
    iconName: 'wallet',
    orderNum: 0,
    tabType: 2,
    status: 1,
  },
  {
    title: '待服务',
    iconName: 'time',
    orderNum: 0,
    tabType: 3,
    status: 1,
  },
  {
    title: '服务中',
    iconName: 'tips',
    orderNum: 0,
    tabType: 4,
    status: 1,
  },
  {
    title: '待确认',
    iconName: 'check-circle',
    orderNum: 0,
    tabType: 4,
    status: 1,
  }
];

const getDefaultData = () => ({
  showMakePhone: false,
  userInfo: {
    avatarUrl: 'https://it00zyq.oss-cn-beijing.aliyuncs.com/1a3306899b294e82ae289b1e843e19a1头像.jpg',
    nickName: 'คิดถึง',
  },
  menuData,
  orderTagInfos,
  customerServiceInfo: {},
  currAuthStep: 1,
  showKefu: true,
  versionNo: '',
  countList: 0,
});

Page({
  data: getDefaultData(),

  onLoad() {
    this.getVersionInfo();
  },

  onShow() {
    this.getTabBar().init();
    this.init();
  },
  onPullDownRefresh() {
    this.init();
  },



  async init() {
    this.fetUserInfoHandle();
    // const res = await getEssayMessage();
    // getInfo().then((res) => {
    //   this.setData({
    //     userInfo: res.data
    //   });
    // });
    // const d = res.data;
    // this.setData({
    //   countList: [0, 0, d.likeCount + d.collectionCount + d.commentCount + d.replyCount, 0]
    // })
  },

  async fetUserInfoHandle() {
    // const res = await getOrderMessage();
    // const d = res.data;
    // const infos = this.data.orderTagInfos;
    // infos[0].orderNum = d.waitPayCount;
    // infos[1].orderNum = d.waitSendCount;
    // infos[2].orderNum = d.waitAcceptCount;
    // infos[3].orderNum = d.waitCommentCount;
    // this.setData({
    //   orderTagInfos: infos
    // })
    

    // fetchUserCenter().then(
    //   ({
    //     userInfo,
    //     countsData,
    //     orderTagInfos: orderInfo,
    //     customerServiceInfo,
    //   }) => {
    //     // eslint-disable-next-line no-unused-expressions
    //     menuData?.[0].forEach((v) => {
    //       countsData.forEach((counts) => {
    //         if (counts.type === v.type) {
    //           // eslint-disable-next-line no-param-reassign
    //           v.tit = counts.num;
    //         }
    //       });
    //     });
    //     const info = orderTagInfos.map((v, index) => ({
    //       ...v,
    //       ...orderInfo[index],
    //     }));
    //     this.setData({
    //       userInfo,
    //       menuData,
    //       orderTagInfos: info,
    //       customerServiceInfo,
    //       currAuthStep: 2,
    //     });
    //     wx.stopPullDownRefresh();
    //   },
    // );
  },

  onClickCell({ currentTarget }) {
    const { type } = currentTarget.dataset;

    switch (type) {
      case 'address': {
        wx.navigateTo({ url: '/pages/usercenter/address/list/index' });
        break;
      }
      case 'essay': {
        wx.navigateTo({ url: '/pages/usercenter/my-essay/index' });
        break;
      }
      case 'collection': {
        wx.navigateTo({ url: '/pages/usercenter/collection/index'});
        break;
      }
      case 'service': {
        this.openMakePhone();
        break;
      }
      default: {
        Toast({
          context: this,
          selector: '#t-toast',
          message: '未知跳转',
          icon: '',
          duration: 1000,
        });
        break;
      }
    }
  },

  onRegister() {
    wxRegister();
  },

  jumpNav(e) {
    const status = e.detail.tabType;

    if (status === 0) {
      wx.navigateTo({ url: '/pages/order/after-service-list/index' });
    } else {
      wx.navigateTo({ url: `/pages/order/order-list/index?status=${status}` });
    }
  },

  jumpAllOrder() {
    wx.navigateTo({ url: '/pages/order/order-list/index' });
  },

  openMakePhone() {
    this.setData({ showMakePhone: true });
  },

  closeMakePhone() {
    this.setData({ showMakePhone: false });
  },

  call() {
    wx.makePhoneCall({
      phoneNumber: this.data.customerServiceInfo.servicePhone,
    });
  },

  gotoUserEditPage() {
    console.log("gotoUserEditPage");
    wx.navigateTo({ url: '/pages/usercenter/person-info/index' });
  },

  getVersionInfo() {
    const versionInfo = wx.getAccountInfoSync();
    const { version, envVersion = __wxConfig } = versionInfo.miniProgram;
    this.setData({
      versionNo: envVersion === 'release' ? version : envVersion,
    });
  },
});
